package cn.edu.cdp.dumpling.service.impl;

import cn.edu.cdp.dumpling.entity.OrderDetail;
import cn.edu.cdp.dumpling.entity.OrderList;
import cn.edu.cdp.dumpling.entity.ShoppingCart;
import cn.edu.cdp.dumpling.mapper.OrderListMapper;
import cn.edu.cdp.dumpling.mapper.ShoppingCartMapper;
import cn.edu.cdp.dumpling.service.OrderDetailService;
import cn.edu.cdp.dumpling.service.OrderListService;
import cn.edu.cdp.dumpling.service.ShoppingCartService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Transactional(rollbackFor = Exception.class)
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    private final OrderDetailService orderDetailService;
    private final OrderListService orderListService;
    private final OrderListMapper orderListMapper;

    public ShoppingCartServiceImpl(OrderDetailService orderDetailService, OrderListService orderListService, OrderListMapper orderListMapper) {
        this.orderDetailService = orderDetailService;
        this.orderListService = orderListService;
        this.orderListMapper = orderListMapper;
    }
    @Override
    public boolean submit(OrderList orderList) {
        // 查询该用户的购物车数据
        LambdaQueryWrapper<ShoppingCart> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(ShoppingCart::getConsumerId, orderList.getConsumerId());
        List<ShoppingCart> shoppingCartList = this.list(wrapper1);

        if (shoppingCartList == null || shoppingCartList.isEmpty()) {
            return false;
        }

        // 计算总金额 totalAmount
        double totalAmount = 0.0;
        for (ShoppingCart item : shoppingCartList) {
            totalAmount += item.getDishPrice() * item.getDishNum();
        }

        // 保存订单（OrderList）
        orderList.setPickupCode(this.createPickupCode());
        orderList.setTotalAmount(totalAmount);
        orderList.setPaymentTime(LocalDateTime.now());
//        orderList.setCompleteTime(LocalDateTime.now());
        orderListService.save(orderList);


        // 保存订单详情
        List<OrderDetail> orderDetailList = shoppingCartList.stream().map((item) -> {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderListId(this.getOrderListId());
            orderDetail.setDishId(item.getDishId());
            orderDetail.setNum(item.getDishNum());
            return orderDetail;
        }).toList();
        orderDetailService.saveBatch(orderDetailList);  // 批量保存

        // 清空购物车
        this.remove(wrapper1);

        return true;
    }

    private int createPickupCode() {
        QueryWrapper<OrderList> wrapper = new QueryWrapper<>();
        wrapper.select("max(pickup_code) as maxCode");
        Map<String, Object> rsMap = orderListMapper.selectMaps(wrapper).get(0);
        return ((Integer) rsMap.get("maxCode")) + 1;
    }

    private int getOrderListId() {
        QueryWrapper<OrderList> wrapper = new QueryWrapper<>();
        wrapper.select("max(id) as maxId");
        Map<String, Object> rsMap = orderListMapper.selectMaps(wrapper).get(0);
        return (Integer) rsMap.get("maxId");
    }
}
